package com.example.my.agiftthat.ui.shop;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;

import com.example.my.agiftthat.R;
import com.example.my.agiftthat.config.Url;
import com.example.my.agiftthat.helper.OkHttpClientHelper;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;

import java.io.IOException;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by My on 2016/12/19.
 */

public class FragmentShop extends Fragment {
    private Context mContext;
    private List<ShopBean.DataBean.ItemsBeanX> items;
    private List<ShopBeans.DataBean.ItemsBean> items1;
    private FragmentAdapters adapter1;
    @Bind(R.id.pullTo_shop)
    PullToRefreshScrollView pullToShop;
    @Bind(R.id.shop1_recyclerView)
    RecyclerView shop1RecyclerView;
    @Bind(R.id.shop2_recyclerView)
    RecyclerView shop2RecyclerView;
    private ShopAdapter adapter;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    ShopBean bean = (ShopBean) msg.obj;
                    items = bean.getData().getItems();
                    adapter = new ShopAdapter(mContext, items);
                    shop1RecyclerView.setAdapter(adapter);
                    pullToShop.onRefreshComplete();
                    break;
                case 1:
                    ShopBeans bean1 = (ShopBeans) msg.obj;
                    items1 = bean1.getData().getItems();
                    adapter1 = new FragmentAdapters(mContext, items1);
                    shop2RecyclerView.setAdapter(adapter1);
                    pullToShop.onRefreshComplete();
                    break;
            }
        }
    };
    private LinearLayoutManager manager;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shop, container, false);
        ButterKnife.bind(this, view);
        return view;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //设置第一个recyclerView
        shop1RecyclerView.setHasFixedSize(true);
        manager = new LinearLayoutManager(mContext);
        shop1RecyclerView.setLayoutManager(manager);
        //设置第二个
        shop2RecyclerView.setHasFixedSize(true);
        GridLayoutManager manager1 = new GridLayoutManager(mContext, 2);
        shop2RecyclerView.setLayoutManager(manager1);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //下载数据解析
        jsonData();
        jsonDatas();
        //下拉刷新
        pullToShop.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                if(!(items == null)&&!(items1 == null)) {
                    Log.i("aaaaa","走了22");
                    adapter.reloadRecyclerView(items, true);
                    adapter1.reloadRecyclerView(items1,true);
                    jsonData();
                    jsonDatas();
                }
                pullToShop.onRefreshComplete();
            }
        });
    }

    private void jsonDatas() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String  run = OkHttpClientHelper.getStringFromURL(mContext,Url.SHOP_TWO,1);
                    Gson gson = new Gson();
                    ShopBeans beanss = gson.fromJson(run,ShopBeans.class);
                    Message msg = handler.obtainMessage();
                    msg.what = 1;
                    msg.obj = beanss;
                    handler.sendMessage(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void jsonData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String run = OkHttpClientHelper.getStringFromURL(getActivity(), Url.SHOP_ONE, 1);
                    Gson gson = new Gson();
                    ShopBean bean = gson.fromJson(run, ShopBean.class);
                    Message msg = handler.obtainMessage();
                    msg.what = 0;
                    msg.obj = bean;
                    handler.sendMessage(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
